package com.lixw.langchain.controller;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: HelloLangChain4JController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/8/22 18:27
 **/
@Slf4j
@RestController
public class HelloLangChain4JController {

    @Resource
    private ChatModel chatModel;

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "question",defaultValue = "你是谁？") String question) {
        String chat = chatModel.chat(question);
        log.info("大模型返回结果: {}", chat);

        UserMessage firstUserMessage = UserMessage.from("Hello, my name is Klaus");
        AiMessage firstAiMessage = chatModel.chat(firstUserMessage).aiMessage(); // Hi Klaus, how can I help you?
        UserMessage secondUserMessage = UserMessage.from("What is my name?");
        AiMessage secondAiMessage = chatModel.chat(firstUserMessage, firstAiMessage, secondUserMessage).aiMessage(); // Klaus
        System.out.println(secondAiMessage.text());
        return chat;
    }
}